package leetcode_core.leetcode_1;

public class SearchBST {
    class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
    }

    public TreeNode searchBST(TreeNode root, int val) {
        if(root == null){
            return root;
        }
        //BST的特性,左小右大
        if(root.val == val){
            return root;
        }
        if(root.val < val){
            //如果要找的值是比root要大的,那么就搜索右子树,因为较大的值在右子树
            return searchBST(root.right,val);
        }
        return searchBST(root.left,val);
    }
}
